package com.etouch.security.controller;


import com.etouch.security.pojo.dto.SysUserDTO;
import com.etouch.security.pojo.entity.SysUser;
import com.etouch.security.pojo.searchargs.SearchSysUserArgs;
import com.etouch.security.service.SysUserService;
import com.etouch.security.util.ResultUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.security.RolesAllowed;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author chenyunchang
 * @since 2020-10-28
 */
@RestController
@RequestMapping("/sysUser")
@Api(tags = "用户管理")
public class SysUserController {

    @Autowired
    private SysUserService sysUserService;

    @ApiOperation("查询用户列表")
    @GetMapping("/list")
    @RolesAllowed({"ROLE_USER", "ROLE_ADMIN"})
    public ResultUtils<SysUserDTO> selectUserList(SearchSysUserArgs args) {
        List<SysUser> sysUserList = sysUserService.selectUserList(args);
        return ResultUtils.success(sysUserList);
    }

    @ApiOperation("查询用户详情")
    @GetMapping("/get")
    @RolesAllowed({"ADMIN"})
    public ResultUtils<SysUserDTO> getUserDetail(@RequestParam(name = "id") Integer id) {
        SysUserDTO sysUserDTO = sysUserService.getSimpleUser(id);
        return ResultUtils.success(sysUserDTO);
    }

    @ApiOperation("查询用户详情,含,角色, 权限菜单信息")
    @GetMapping("/userDetail")
    @RolesAllowed({"ADMIN","USER"})
    public ResultUtils<SysUserDTO> userDetail(SearchSysUserArgs args) {
        SysUserDTO userDetail = sysUserService.getUserDetail(args);
        return ResultUtils.success(userDetail);
    }

    @ApiOperation("编辑用户")
    @GetMapping("/update")
    @RolesAllowed({"ADMIN"})
    public ResultUtils<SysUserDTO> update(SysUser sysUser) {
        sysUserService.updateById(sysUser);
        return ResultUtils.success();
    }

    @ApiOperation("删除用户")
    @GetMapping("/delete")
    @RolesAllowed({"ADMIN"})
    public ResultUtils<SysUserDTO> delete(SysUser sysUser) {
        sysUserService.delete(sysUser.getId());
        return ResultUtils.success();
    }

    @ApiOperation("新增用户")
    @GetMapping("/save")
    @RolesAllowed({"ADMIN"})
    public ResultUtils<SysUserDTO> save(SysUserDTO sysUserDTO) {
        sysUserService.saveSysUser(sysUserDTO);
        return ResultUtils.success();
    }
}

